package com.yhgm.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhgm.annotation.AuthCheck;
import com.yhgm.common.BaseResponse;
import com.yhgm.common.DeleteRequest;
import com.yhgm.common.ErrorCode;
import com.yhgm.common.ResultUtils;
import com.yhgm.constant.UserConstant;
import com.yhgm.exception.BusinessException;
import com.yhgm.exception.ThrowUtils;
import com.yhgm.model.dto.role.RoleAddRequest;
import com.yhgm.model.dto.role.RoleAssignPremRequest;
import com.yhgm.model.dto.role.RoleQueryRequest;
import com.yhgm.model.dto.role.RoleUpdateRequest;
import com.yhgm.model.entity.RolePermission;
import com.yhgm.model.entity.Roles;
import com.yhgm.model.vo.RoleVO;
import com.yhgm.service.RolePermissionService;
import com.yhgm.service.RolesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: yh
 * @Date: 2024/3/7
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RolesController {
    @Resource
    public RolesService rolesService;

    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<RoleVO> getRoleById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Roles roles = rolesService.getById(id);
        RoleVO roleVO = new RoleVO();
        roleVO = roleVO.beanToVO(roles);
        return ResultUtils.success(roleVO);
    }

    @GetMapping("/get/list")
    public BaseResponse<List<Roles>> getRoleList() {
        List<Roles> list = rolesService.list();
        ThrowUtils.throwIf(list == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(list);
    }

    /**
     * 分页获取
     * @param roleQueryRequest
     * @return
     */
    @PostMapping("/get/page")
    public BaseResponse<Page<Roles>> getRolePage(@RequestBody RoleQueryRequest roleQueryRequest) {
        int current = roleQueryRequest.getCurrent();
        int pageSize = roleQueryRequest.getPageSize();
        Page<Roles> rolesPage = rolesService.page(new Page<>(current, pageSize),
                rolesService.getQueryWrapper(roleQueryRequest));
        return ResultUtils.success(rolesPage);
    }

    /**
     * 添加角色，只能由超级管理员操作
     * @param roleAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_PLUS_ROLE)
    public BaseResponse<Long> addRole(@RequestBody RoleAddRequest roleAddRequest) {
        if (roleAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Roles role = new Roles();
        BeanUtils.copyProperties(roleAddRequest, role);
        boolean result = rolesService.save(role);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(role.getId());
    }

    /**
     * 修改角色信息，只有超级管理员可操作
     * @param roleUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_PLUS_ROLE)
    public BaseResponse<Boolean> updateRole(@RequestBody RoleUpdateRequest roleUpdateRequest) {
        if (roleUpdateRequest == null || roleUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Roles role = new Roles();
        BeanUtils.copyProperties(roleUpdateRequest, role);
        boolean result = rolesService.updateById(role);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 删除角色，超级管理员可操作
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_PLUS_ROLE)
    public BaseResponse<Boolean> deleteRole(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = rolesService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 给角色分配权限点
     * @param roleAssignPremRequest
     * @return
     */
    @PostMapping("/assignPrem")
    @AuthCheck(mustRole = UserConstant.ADMIN_PLUS_ROLE)
    public BaseResponse<Boolean> assignPrem(@RequestBody RoleAssignPremRequest roleAssignPremRequest) {
        if (roleAssignPremRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = roleAssignPremRequest.getId();
        List<Long> permIds = roleAssignPremRequest.getPermIds();
        String ids = JSONUtil.toJsonStr(permIds);
        UpdateWrapper<Roles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("permIds", ids);
        rolesService.update(updateWrapper);
        return ResultUtils.success(true);
    }
}
